using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using MediaWikiEngine.Language;

namespace Commonplace.Domain
{
    public abstract class DetailRecord
    {
        string _filename;
        string _name;
        string _description = "";
        string _source = "";
        string _license = "";
        string _categories = "";
        bool _watch = false;
        string _date = "";
        string _author = string.Empty;

        bool _uploaded = false;

        public virtual string Filename 
        { 
            get 
            { 
                return _filename; 
            } 
            set 
            { 
                _filename = value;

                string strippedFilename = Path.GetFileNameWithoutExtension(_filename);
                Name = strippedFilename.Replace("_", " ");

                OnUpdated(); 
            } 
        }
        public string Name { get { return _name; } set { _name = value; OnUpdated(); } }
        public string Description { get { return _description; } set { _description = value; OnUpdated(); } }
        public string Source { get { return _source; } set { _source = value; OnUpdated(); } }
        public string License { get { return _license; } set { _license = value; OnUpdated(); } }
        public string Categories { get { return _categories; } set { _categories = value; OnUpdated(); } }
        public bool Watch { get { return _watch; } set { _watch = value; OnUpdated(); } }
        public string Author { get { return _author; } set { _author = value; OnUpdated(); } }
        public string Date { get { return _date; } set { _date = value; OnUpdated(); } }

        public event EventHandler Updated;

        /// <summary>
        /// Gets or sets an indicator of whether this record has been completely filled in.
        /// </summary>
        public bool RecordComplete
        {
            get
            {
                if (Name.Trim().Length > 0
                    && Description.Trim().Length > 0
                    && Source.Trim().Length > 0
                    && Author.Trim().Length > 0
                    //can deal without license, categories, and date, for now
                    )
                    return true;

                //else
                return false;
            }
        }

        public bool Uploaded
        {
            get { return _uploaded; }
            set { _uploaded = value; OnUpdated(); }
        }

        public string RecordDescription
        {
            get
            {
                StringBuilder recordDescription = new StringBuilder();
                recordDescription.AppendFormat("{0}\n\n", Description);
                recordDescription.AppendFormat("Author: {0}\n\n", Author);
                recordDescription.AppendFormat("Source: {0}\n\n", Source);
                recordDescription.AppendFormat("Date: {0}\n\n", Date);

                string[] categorySplit = Categories.Split('|');
                foreach (string category in categorySplit)
                    recordDescription.AppendFormat("[[{0}:{1}]]", LanguageProvider.Instance.Namespaces["Category"], category.Trim());

                return recordDescription.ToString();
            }
        }

        public DetailRecord(string filename)
        {
            Filename = filename;
        }

        public DetailRecord()
        {
        }

        public abstract DetailControl CreateDetailControl();

        protected virtual void OnUpdated()
        {
            if (null != Updated)
                Updated(this, EventArgs.Empty);
        }

        public string InformationTemplate
        {
            get
            {
                string informationTemplateFormat = "{{{{Information\n|Description={0}\n|Source={1}\n|Date={2}\n|Author={3}\n|Permission=\n|other_versions=\n}}}}\n\n";

                StringBuilder recordDescription = new StringBuilder();
                recordDescription.AppendFormat(informationTemplateFormat, Description, Source, Date, Author);

                string[] categorySplit = Categories.Split('|');
                foreach (string category in categorySplit)
                    recordDescription.AppendFormat("[[{0}:{1}]]", LanguageProvider.Instance.Namespaces["Category"], category.Trim());

                return recordDescription.ToString();
            }
        }

    }
}
